//<script type='text/javascript'>
 
/////////// SOLUTION FOR THE CONSOLE.LOG() BUG /////////////
if(!console) var console = {log : function( string ){ dashboard.comment( string ) }}


////////// CONTROLS //////////
WinControl = function() {
	Object.extend(control = new Element("div", {id:'control_win', class:"win_ctrl", style:"display:none;"}), controls.win);
	control.insert( new Element("input", { type:"button", class:"ico drag"}) );
	control.insert( new Element("input", { type:"button", class:"ico mini"}).observe('click', control.minimize) );
	control.insert( new Element("input", { type:"button", class:"ico close"}).observe('click', control.close) );
		
	return control;
}

TextControl = function() {
	Object.extend( control = new Element("div", {id:'control_text', class:"text_ctrl", style:"display:none;"}), controls.text );
	
	init.langs.each( control.seedlang.bind( control ));
	control.insert( new Element( "span", { class:"name" }));
	control.insert( new Element( "input", { type:"field" }));
		
	return control;
}

FlowControl = function() {
	Object.extend(control = new Element("div", {id:'control_list', class:"flow_ctrl", style:"display:none;"}), controls.flow);
	
	control.insert( count = new Element("div", { class:"count"}) );
	count.insert( new Element("div", { id:"flow.back", class:"arrow"}).observe('click', control.prev) );
	count.insert( new Element("span", { id:"flow.text"}).update("calculating") );
	count.insert( new Element("div", { id:"flow.next", class:"arrow right"}).observe('click', control.next) );
	control.insert( new Element("div", { id:"flow.order", class:"order"}).update( "onbepaald" ).observe('click', control.order) );
		
	return control;
}

FloatControl = function() {
	Object.extend(control = new Element("div", {id:'control_float', class:"float_ctrl", style:"display:none;"}), controls.float );
	
	control.appendChild( new Element("img",{'id':"linefloat.plus",src: engine.path.img+"ico.plus.png",'class':"ico"}) );//.observe("click",function(){ win.contr.linefloat.current.plus() }));
	//control.appendChild( new Element("img",{'id':"linefloat.alt",src: engine.path.img+"ico.alt.png",'class':"ico"}) );//.observe("click",function(){ win.contr.linefloat.current.clone() }));
	control.appendChild( new Element("img",{'id':"linefloat.kill",src: engine.path.img+"ico.close.png",'class':"ico"}).observe("click", control.kill ));
	
	return control;
}



///////// CONTROLS EXTENTIONS GROUPING OBJECT ////////////
controls = {
		//// add control to child
		bubble	: function(e) {
			if(!(control = $('control_' + this.objtype)) || control._current != this) {
				switch (this.objtype) {
				case "win" : 
					if(!control) document.body.insert( new WinControl() );
					break;
				case "list" :
					if(!control) document.body.insert( new FlowControl() );
					this.observe( "mouseout", controls.blur );
					break;
				case "float" :
					if(!control) document.body.insert( new FloatControl() );
					this.observe( "mouseout", controls.blur );
					break;
				case "text" :
					if(!control) document.body.insert( new TextControl() );
					this.observe( "mouseout", controls.blur );
					break;
				}
				
				$('control_' + this.objtype)._focus( this );
				
			}
		},
		position	: function(obj) {
			$('control_' + obj.objtype)._position(obj);
			if(obj.objtype == "list") $("linefloat").hide(); 
		},
		blur	: function(e) {
			control = ( e.objtype )? $('control_' + e.objtype) : $('control_' + this.objtype);
			el = (this.objtype == "list")? ((e.relatedTarget !=  $("linefloat"))? 0 : 1):0;
			if(e.objtype || (control.visible() && e.relatedTarget != control && !e.relatedTarget.descendantOf(this) && e.relatedTarget != this && !el) ) {
				if(this.objtype == "list") $("linefloat").hide(); 
				control._current = 0;
				control.hide();
			}
		},
		//// the different control prototypes
		win		: {
			_current	: {},
			_focus		: function(obj) {
				this.show();
				this.clonePosition(obj, {setWidth:false, setHeight:false, offsetLeft:390, offsetTop:4});
				this._current = obj;
			},
			minimize		: function(e) {
				this.parentNode._current.firstChild.toggle();
			},
			close		: function(e) {
				this.parentNode._current.remove();
				this.parentNode.hide();
				delete win.all[this.parentNode._current.id];
			}
		},
		
		text		: {
			_current	: {},
			_focus		: function( obj ) {
				this.show();
				this.clonePosition(obj, {setWidth:false, setHeight:false, offsetLeft:0, offsetTop:0 });
				this._current = obj;
			},
			seedlang	: function( lang ) {
				this.insert( el = new Element( "span", { class:"lang" }).update( lang ).observe( "click", this.swaplang.bind( this )));
				if( lang == init.lang ) el.addClassName( "select" );
			},
			swaplang	: function( e ) {
        		lang	= e.relatedTarget;
        		
        		$$( ".textfield" ).invoke( "reload", this.select( ".lang.select" )[0].nodeValue, lang.nodeValue );
        		$$( ".htmlfield" ).invoke( "reload", this.select( ".lang.select" )[0].nodeValue, lang.nodeValue );
        		
        		$$( ".lang" ).invoke( "removeClassName", "select" );
        		lang.addClassName( "select" );
    		}

		
		},
		
		flow		: {
			_current	: {},
			_order_options	: { "ID_desc":["id teruglopend", "ID", "desc"], "ID_asc":["id oplopend", "ID", "asc"],  "short_asc":["alfabetisch", "short", "asc"],   "short_desc":["alfabetisch z-a", "short", "desc"], "pdate_desc":["datum", "pdate", "desc"] },
			_order_current	: "ID_desc",
			_focus		: function(obj) {
				this._position(obj);
				this._current = obj;
				if( obj.vars.flow ) this._visualise(obj); else this._request(obj);
				
			},
			_position		: function(obj) {
				this.clonePosition(obj, {setHeight:false, offsetLeft:0, offsetTop:obj.getHeight()-3 });				
			},
			_visualise	: function(obj) {
				if(obj.vars.flow.total > obj.vars.flow.limit) {
					this.show();
					till = (obj.vars.flow.current + obj.vars.flow.limit);
					$('flow.text').update(obj.vars.flow.current + " till " + ((till > obj.vars.flow.total)? obj.vars.flow.total:till) );
					$('flow.text').insert( new Element("span").update(obj.vars.flow.total + " total") );
					opt = this._order_current = (obj.vars.flow.order)? $H(obj.vars.flow.order).keys()[0] +"_"+ $H(obj.vars.flow.order).values()[0] : this._order_current;
					$('flow.order').update(this._order_options[ opt ][0]);
				}

			},
			_request	: function(obj) {
				if(!obj) obj = this._current;
				xhttp.send('control.call.php', {trg:obj.id, wtype:'push', tool:obj.vars.tool, action:"flow.request", vars:obj.vars });
			},
			_push		: function(obj, values) {
				if(typeof obj == "string") obj = $(obj);
				///// CHECK FOR LIST UPDATE
				if(obj.vars.flow != values) $('control_list')._current.request(values);
				new Effect.Highlight( 'flow.text', { startcolor:'#000000', endcolor:'#777777', duration:0.8 });
				this._visualise(obj);
			},
			order		: function(e) {
				keys = $H(this.offsetParent._order_options).keys();
				cur = keys.indexOf(this.offsetParent._order_current);
				opt = this.offsetParent._order_current = keys[ (cur < keys.length-1)? cur+1 : 0 ];
				$('flow.order').update(this.offsetParent._order_options[ opt ][0]);
				this.offsetParent._current.vars.flow.order = {};
				this.offsetParent._current.vars.flow.order[this.offsetParent._order_options[ opt ][1]] = this.offsetParent._order_options[ opt ][2];
				this.offsetParent._current.vars.flow.current = 0;
				this.offsetParent._request();
			},
			prev		: function(e) {
				obj = this.offsetParent._current;
				xhttp.send('control.call.php', {trg:obj.id, wtype:'push', tool:obj.vars.tool, action:"flow.prev", vars:obj.vars });
			},
			next		: function(e) {
				obj = this.offsetParent._current;
				xhttp.send('control.call.php', {trg:obj.id, wtype:'push', tool:obj.vars.tool, action:"flow.next", vars:obj.vars });
			}
		},
		
		float		: {
			_current	: {},
			_focus		: function(obj) {
				this.show();
				this.clonePosition(obj, {setWidth:false, setHeight:false, offsetLeft:obj.getWidth()-this.getWidth()-1, offsetTop:3});
				this._current = obj;
			},
			minimize	: function(e) {
				this.parentNode._current.firstChild.toggle();
			},
			kill	: function() {
				li = this.up(0)._current;
				delete float.li.all[ li.id ];
				
				xhttp.send('control.call.php', {trg:li.up(1).id+".comment", wtype:'push', tool:li.vars.tool, action:"kill", 'vars':li.vars});
				controls.blur( li );
				li.remove();
    		}	
		}

	}
	
	 



// FLOAT OBJECT
win.contr = {
	init	: function() {
        $('body').appendChild(new Listfloat());
                
        linefloat = new Element("div", {id:"linefloat", class:"linefloat", style:"display:none;"});
        $('body').appendChild(linefloat);
        
		linefloat.appendChild(new Element("img",{'id':"linefloat.plus",src: engine.path.img+"ico.plus.png",'class':"ico"}).observe("click",function(){ win.contr.linefloat.current.plus() }));
		linefloat.appendChild(new Element("img",{'id':"linefloat.alt",src: engine.path.img+"ico.alt.png",'class':"ico"}).observe("click",function(){ win.contr.linefloat.current.clone() }));
		linefloat.appendChild(new Element("img",{'id':"linefloat.kill",src: engine.path.img+"ico.close.png",'class':"ico"}).observe("click",function(){ win.contr.linefloat.current.kill() }));
		// this.plus();
		//win.contr.linefloat.current
    },
    
    //LISTFLOAT (list/dropbox control)
	listfloat	: {
    	current	: 0,
    	fx		: { name:'Highlight', options:{startcolor:'#000000', endcolor:'#777777', restorecolor:'#777777', duration:0.5} },
    	hook	: function(trg) {
    		if(typeof trg == "string") trg = $(trg);
    		type = id.split('.').pop();
    		vars = win.asset[type].list[trg.id];
    		
    		$("listfloat").clonePosition(id, {setWidth:false, setHeight:false, offsetLeft:0, offsetTop:$(id).getHeight()-6});

			win.asset.objlist.current = trg.id;
    		if(!$('listfloat').visible()) $('listfloat').show();
    	},
    	flow	: function(set) {
    		vars = win.contr.listfloat.current.vars;
    		
    		//$(id+".box").setStyle({height:$(id+".box").getHeight()+"px"});
    	},
    	oldflow	: function(n,btn) {
        id = btn.parentNode.parentNode.id;
        vars = win.asset.dropbox.list[id];
         
        $(id+".box").update("loading...");
        
        vars.flow.cur = win.asset.dropbox.list[id].flow.cur = (vars.flow.cur+n <=0)? Math.ceil(vars.flow.total/vars.flow.itv)-1 : ((vars.flow.cur+n>=Math.ceil(vars.flow.total/vars.flow.itv))?0:vars.flow.cur+n);
        xhttp.send('control.call.php', {trg:btn.parentNode.id+".comment", wtype:'push', tool:vars.tool, action:'list.update', 'vars':vars});
        win.float.list = $H(win.float.list);
        $(id+".box").childElements().each( function(e){ win.float.list.unset(e.id); });
   		},
		updateStatus	: function (trg) {
			if(trg && this.current!=trg) this.current = trg;
			//if(string = this.current.vars.flow.current+" - "+(this.current.vars.flow.current+this.current.vars.floatlist.length)+" van "+this.current.vars.flow.total != $("listfloat.flow.comment").innerHTML)
    		xhttp.gfx( this.current.vars.flow.current+" - "+(this.current.vars.flow.current+this.current.vars.floatlist.length)+" van "+this.current.vars.flow.total, "listfloat.flow.comment", this.fx);
		}	
    },
	linefloat	: {
    	current	: 0,
    	hook	: function(trg) {
    		/*if(typeof trg == "string") trg = $(trg);
    		type = id.split('.').pop();
    		vars = win.asset[type].list[trg.id];
    		
    		$("listfloat").clonePosition(id, {setWidth:false, setHeight:false, offsetLeft:0, offsetTop:$(id).getHeight()-6});

			win.asset.objlist.current = trg.id;
    		if(!$('listfloat').visible()) $('listfloat').show();*/
    	}
    }
}

win.contr.buttonline	= function (buttons) {
	obj = new Element("table", {id:"listfloat.display.table", class:"buttonline", cellpadding:0, cellspacing:0});
	obj.vars = vars;
    tbody =	new Element("tbody");
	tr = new Element("tr");
	tbody.appendChild(tr);
	obj.appendChild(tbody);
        
	twidth = obj.getWidth();
	buttons.each(function(obj) {
		td = new Element("td", {style:"width:"+Math.floor(100/buttons.length)+"%"});
		tr.appendChild(td);
		if(obj) { obj.assetref = vars.assetref;
			ip = new Element('input',{id:obj.id, type:obj.type, value:obj.descr, style:"width:"+Math.ceil(twidth/buttons.length)+"px;"});
			td.appendChild(ip);
			ip.observe('click',(obj.func)? obj.func : function(){ /*win.asset.buttonline.update(this.id);*/ });
        }
	});
}


///////////////////// ALL YOUR CONTR OBJECT BELONG TO US
var Listfloat = function () {
	Object.extend(obj = new Element("div", {id:"listfloat", class:"listfloat", style:"display:none"}), win.contr.listfloat);
	
	obj.appendChild(new Element("div", {id:"listfloat.descr",'class':"descr"}));
	obj.appendChild(flow = new Element("div", {id:"listfloat.flow", class:"flow"}));
	flow.appendChild(new Element("input", {id:"listfloat.flow.next", type:'button', class:'ico next', style:'float:right'}).observe('click',function(){ $('listfloat').current.flow(1); }));
	flow.appendChild(new Element("input", {id:"listfloat.flow.prev", type:'button', class:'ico prev'}).observe('click',function(){ $('listfloat').current.flow(-1); }));
	flow.appendChild(new Element("font", {id:"listfloat.flow.comment", style:'position:relative; left:12px; top:-5px;'}).update("..."));
	obj.appendChild(new Element("table", {id:"listfloat.display", cellpadding:0, cellspacing:0}));
                    
	obj.observe("mouseout",function(event){
		if(!event.relatedTarget.descendantOf(this) && event.relatedTarget != this) win.asset.objlist.blurfloat();
	});

	
	return obj;
}



